//
//  JDCashoutSuccessViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2025/4/28.
//

import UIKit
import SwiftyJSON
/// 提现成功页面
class JDCashoutSuccessViewController: JDBaseViewController {

    var successImageV: UIImageView?
    var successLabel: UILabel?
    var cashoutNumberLabel: UILabel?
    var cashoutDescriptionLabel: UILabel?
    var finishButton: UIButton?
    var checkRecordButton: UIButton?
    
    var cashoutNumber: String = ""

    
    var cashoutType: String = ""
    
    var cashoutNicknameString: String = ""

  
//    override func viewWillAppear(_ animated: Bool) {
//        super.viewWillAppear(animated)
//        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
//    }
//    
//    override func viewWillDisappear(_ animated: Bool) {
//        super.viewWillDisappear(animated)
//        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
//    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupConfig()
        self.setupUIElements()
        
//        self.navigationController?.interactivePopGestureRecognizer?.delegate = nil // 如果需要处理其他手势冲突
        
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupLeftBackItem()
    }
    
    func setupUIElements() {
    
        self.successImageV = UIImageView()
        self.view.addSubview(self.successImageV!)
        self.successImageV?.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(18+(statusBarHeight+44))
            make.size.equalTo(CGSize(width: 132, height: 132))
        }
        self.successImageV?.image = UIImage(named: "cashout_success")?.withRenderingMode(.alwaysOriginal)
        
        self.successLabel = UILabel()
        self.view.addSubview(self.successLabel!)
        self.successLabel?.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.successImageV!.snp.bottom).offset(3)
            make.height.equalTo(20)
        }
        self.successLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 14, weight: .semibold), textColor: color("#00C86C"), numberOfLines: 1)
        self.successLabel?.text = "申请提现成功"
        
        self.cashoutNumberLabel = UILabel()
        self.view.addSubview(self.cashoutNumberLabel!)
        self.cashoutNumberLabel?.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.successLabel!.snp.bottom).offset(3)
            make.height.equalTo(45)
        }
        self.cashoutNumberLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 32, weight: .semibold), textColor: color("#333333"), numberOfLines: 1)
        self.cashoutNumberLabel?.text = String(format: "￥%.2f", JSON(cashoutNumber as Any).doubleValue)
        self.cashoutNumberLabel?.priceString(UIFont.systemFont(ofSize: 18, weight: .semibold), UIFont.systemFont(ofSize: 32, weight: .semibold))
        
        self.cashoutDescriptionLabel = UILabel()
        self.view.addSubview(self.cashoutDescriptionLabel!)
        self.cashoutDescriptionLabel?.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.cashoutNumberLabel!.snp.bottom).offset(4)
            make.height.equalTo(17)
        }
        self.cashoutDescriptionLabel?.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 12, weight: .regular), textColor: color("#9DA1B2"), numberOfLines: 1)
        if cashoutType == "wxpay" {
            self.cashoutDescriptionLabel?.text = String(format: "提现申请已经提交到微信(昵称:%@)", cashoutNicknameString)
        } else if cashoutType == "alipay" {
            self.cashoutDescriptionLabel?.text = String(format: "提现申请已经提交到支付宝(昵称:%@)", cashoutNicknameString)
        }
        
        self.finishButton = UIButton(type: .custom)
        self.view.addSubview(self.finishButton!)
        self.finishButton?.snp.makeConstraints { make in
            make.leading.equalTo(self.view).offset(57)
            make.trailing.equalTo(self.view).offset(-57)
            make.top.equalTo(self.cashoutDescriptionLabel!.snp.bottom).offset(26)
            make.height.equalTo(50)
        }
        self.finishButton?.makeCorner(25)
        self.finishButton?.buttonConfig(normalTitle: "完成", normalImageNamed: "", normalTitleColor: color("#FFFFFF"), titleFont: UIFont.systemFont(ofSize: 16, weight: .regular), backgroundColor: color("#333333"))
        self.finishButton?.addTarget(self, action: #selector(doneAction), for: .touchUpInside)
        
        self.checkRecordButton = UIButton(type: .custom)
        self.view.addSubview(self.checkRecordButton!)
        self.checkRecordButton?.snp.makeConstraints { make in
            make.leading.equalTo(self.view).offset(57)
            make.trailing.equalTo(self.view).offset(-57)
            make.top.equalTo(self.finishButton!.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        self.checkRecordButton?.buttonConfig(normalTitle: "账单查看", normalImageNamed: "", normalTitleColor: color("#333333"), titleFont: UIFont.systemFont(ofSize: 16, weight: .regular), backgroundColor: color("#FFFFFF"))
        self.checkRecordButton?.makeCorner(25)
        self.checkRecordButton?.addTarget(self, action: #selector(checkRecordAction), for: .touchUpInside)
    }
    
    @objc func doneAction() {
        print("完成")
//        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
//        let orderList = JDOrderListViewController()
//        orderList.isFromPaySuccess = true
//        orderList.currentIndex = 3
//        self.hidesBottomBarPushViewController(orderList)
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func checkRecordAction() {
        print("账单查看")
//        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
//        let orderList = JDOrderListViewController()
//        orderList.isFromPaySuccess = true
//        orderList.currentIndex = 3
//        self.hidesBottomBarPushViewController(orderList)

        if (self.navigationController?.viewControllers.count ?? 0) > 1 {
            guard let popVC = self.navigationController?.viewControllers[1] else { return  }
            self.navigationController?.popToViewController(popVC, animated: true)
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
